package com.xnyzc.lhy.mis.util;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class XieChengUtil {

    public static String getCallbackSign(String vendorID, String timeStamp, int length, String secrectKey) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(vendorID);
        buffer.append(timeStamp);
        buffer.append(secrectKey);
        buffer.append(length);
        String res = md5(buffer.toString());
        log.info("XC-" + buffer.toString() + "-" + res);
        return res.toLowerCase();
    }

    public static String getSign(String vendorID, String channel, String version, String timeStamp, int length, String secrectKey) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(vendorID);
        buffer.append(version);
        buffer.append(channel);
        buffer.append(timeStamp);
        buffer.append(secrectKey);
        buffer.append(length);
        String res = md5(buffer.toString());
        log.info("XC-" + buffer.toString() + "-" + res);
        return res.toLowerCase();
    }

    /**
     * md5 加密
     *
     * @param input
     * @return
     */
    public static String md5(String input) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
        }

        md.update(input.getBytes());
        byte byteData[] = md.digest();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            buffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        return buffer.toString().toUpperCase();
    }

    public static String encodeDES(String key, String string) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "DES"));
        byte[] bytes = cipher.doFinal(string.getBytes());
        return byteToHex(bytes);
    }

    public static String decodeDES(String key, String string) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "DES"));
        byte[] bytes = cipher.doFinal(hex2byte(string));
        return new String(bytes, "UTF-8");
    }

    /**
     * byte数组转hex
     *
     * @param bytes
     * @return
     */
    public static String byteToHex(byte[] bytes) {
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().trim();
    }

    /**
     * 十六进转二进制
     *
     * @param hexStr 待转换16进制字符串
     * @return 二进制字节组
     */
    public static byte[] hex2byte(String hexStr) {
        if (hexStr == null) {
            return null;
        }
        hexStr = hexStr.trim();
        int len = hexStr.length();
        if (len == 0 || len % 2 == 1) {
            return null;
        }
        byte[] digest = new byte[len / 2];
        try {
            for (int i = 0; i < hexStr.length(); i += 2) {
                digest[i / 2] = (byte) Integer.decode("0x" + hexStr.substring(i, i + 2)).intValue();
            }
            return digest;
        } catch (Exception e) {
            return null;
        }
    }
}
